#!/bin/bash

#Decs:set lvs type of net
#author:zhang huan huan
#create:2020-09-11
#update:
#usage:

dip=
vip_port_str=
rip_ports=
dstype=
checkflag=false 
ens33file=/etc/sysconfig/network-scripts/ifcfg-ens33 

#net ds 设置
dsserverset(){
	read -p "请输入VIP和端口号：" vip_port_str 
	read -p "请输入read server数目:"  num
	for i in `seq 1 $num`
	do	
		read -p "请输入第$i个read server ip和端口："  rip_ports[$i] 
	done
	read -p "请输入调度类型：" dstype

	yum -y install ipvsadm

	#set virtual server
	ipvsadm -A -t $vip_port_str -s $dstype
	#add read server
	for i in `seq 1 $num`
	do
		ipvsadm -a -t $vip_port_str -r ${rip_ports[$i]} -m
	done

	#open net.ipv4.ip_forward=1
	echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
}

#net read server设置
readserverset(){
	read -p "请输入GATEWAY:" dip
	#transition
	route del default
	route add default gw $dip

	#permanent
	# sed -i "s/GATEWAY.*/GATEWAY=$dip/" $ens33file
	# echo "设置成功，正常重启网卡。。。"
	# ifdown ens33
	# ifup ens33
}  

#dr ds 配置
dr_serverset(){ 
	read -p "请输入VIP：" vip_port_str
	#transition
	ifconfig ens33:0 $vip_port_str broadcast $vip_port_str netmask 255.255.255.255 up
	#可加可不加，老版本的系统要加
	route add -host $vip_port_str dev ens33:0
	
	#permanent
	# sed -i "s/GATEWAY.*/GATEWAY=$dip/" $ens33file
	# echo "设置成功，正常重启网卡。。。"
	# ifdown ens33
	# ifup ens33 

	read -p "请输入read server数目:" num
	for i in `seq 1 $num`
	do	
		read -p "请输入第$i个read server ip："  rip_ports[$i] 
		checkip ${rip_ports[$i]} "false"
	done

	read -p "请输入调度类型：" dstype
	#install ipvsadm
	yum -y install ipvsadm

	ipvsadm -C
	#set virtual server
	ipvsadm -A -t $vip_port_str -s $dstype

	#add read server
	for i in `seq 1 $num`
	do
		ipvsadm -a -t $vip_port_str -r ${rip_ports[$i]} -g
	done
}

#dr read 配置
dr_readset(){
	read -p "请输入VIP：" vip_port_str
	ifconfig lo:0 $vip_port_str  broadcast $vip_port_str netmask 255.255.255.255 up
	#可加可不加，老版本的系统要加
	route add -host $vip_port_str dev lo:0

	#transtion
	echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
	echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
	echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
	echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
	
	#permanent
	# 		cat >/etc/sysctl.conf <<EOF
	# net.ipv4.conf.all.arp_ignore=1
	# net.ipv4.conf.lo.arp_ignore=1
	# net.ipv4.conf.all.arp_announce=2
	# net.ipv4.conf.lo.arp_announce=2
	# EOF
	# 		sysctl -p	
}
	 
while :
do	
	echo "---------------LVS模式选择NET/DR--------------------------" 
	echo "--------------------NET设置------------------------------" 
	echo "---------------1） 设置director server-------------------"
	echo "---------------2） 设置read     server-------------------" 
	echo "--------------------DR设置--------------------------------" 
	echo "---------------3） 设置director server-------------------"
	echo "---------------4） 设置read     server-------------------" 
	echo "--------------------q|Q 退出------------------------------"
	read -p "请输入：" chis 
	case $chis in
		1)
			dsserverset
			break
			;;
		2)
			readserverset
			break
			;;  
		3)
			dr_serverset
			break
			;;
		4)
			dr_readset
			break
			;; 
		*)
		q|Q)
			exit
			;;
		*)
			break
			;;
	esac
done